<?php
// 修复路径问题 - 使用绝对路径
$apiDir = __DIR__;
$projectRoot = dirname($apiDir);

require_once $projectRoot . '/config/config.php';
require_once $projectRoot . '/includes/database.php';

// 设置响应头
header('Content-Type: application/json; charset=utf-8');

try {
    // 初始化数据库
    $db = new Database();

    // 获取请求参数
    $categoryIds = isset($_GET['category_ids']) ? explode(',', $_GET['category_ids']) : [];

    // 过滤和验证分类ID
    $validCategoryIds = [];
    foreach ($categoryIds as $categoryId) {
        $categoryId = (int)trim($categoryId);
        if ($categoryId > 0) {
            $validCategoryIds[] = $categoryId;
        }
    }

    if (empty($validCategoryIds)) {
        throw new Exception('无效的分类ID');
    }

    // 获取标签数据
    $tags = $db->getTagOptionsByCategories($validCategoryIds);

    // 返回成功响应
    echo json_encode([
        'success' => true,
        'tags' => $tags,
        'total' => count($tags)
    ], JSON_UNESCAPED_UNICODE);

} catch (Exception $e) {
    // 返回错误响应
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage(),
        'tags' => [],
        'total' => 0
    ], JSON_UNESCAPED_UNICODE);
}
?>